﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SemWeb;

namespace OntologyUtility
{
    public class BuildTree
    {
        private String path = "";
        private Node concept;

        public BuildTree(String path) {
            this.path = path;
        }

        public void readAndParse() {
            concept = new Node();
            concept.Name = "Concept";

            try
            {

                MemoryStore store = new MemoryStore(
                    new RdfXmlReader(path));

                int index = 0;

                foreach (Statement statement in store)
                {
                    if (index > 1)
                    {

                        String predicate = statement.Predicate.Uri.Split('#')[1];
                        String subject = statement.Subject.Uri.Split('#')[1];
                        String objectT = statement.Object.Uri.Split('#')[1];

                        if (objectT.Equals("Concept"))
                        {
                            Node _node = new Node();
                            _node.Name = subject;
                            _node.Father = null;
                            _node.Type = objectT;
                            concept.Childs.Add(_node);
                        }
                    }
                    index++;
                }

                recursiveParse(ref concept, store);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void recursiveParse(ref Node node2Iterate, MemoryStore store)
        {


            //scorro alla ricerca dei figli
            for (int i = 0; i < node2Iterate.Childs.Count; i++)
            {
                int index = 0;
                Node node = (Node)node2Iterate.Childs[i];
                foreach (Statement statement in store)
                {
                    if ((index++) > 1)
                    {
                        String predicate = statement.Predicate.Uri.Split('#')[1];
                        String subject = statement.Subject.Uri.Split('#')[1];
                        String objectT = statement.Object.Uri.Split('#')[1];

                        if (predicate.Equals("hasChild") && subject.Equals(node.Name))
                        {
                            Node _node = new Node();
                            _node.Name = objectT;
                            _node.Father = node;
                            searchType(ref _node, store);
                            node.Childs.Add(_node);
                        }
                    }
                }

                recursiveParse(ref node, store);
            }
        }

        private void searchType(ref Node node, MemoryStore store)
        {
            int index = 0;

            foreach (Statement statement in store)
            {
                if ((index++) > 1)
                {
                    String predicate = statement.Predicate.Uri.Split('#')[1];
                    String subject = statement.Subject.Uri.Split('#')[1];
                    String objectT = statement.Object.Uri.Split('#')[1];

                    if (subject.Equals(node.Name) && predicate.Equals("hasFeatureType"))
                    {
                        node.Type = objectT;
                        return;
                    }
                }
            }

        }

        public Node ConceptNode 
        {
            get { return concept; }
            set { concept = value; }
        }

        public String Path
        {
            get { return path; }
            set { path = value; }
        }

    }
}
